/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CM_RDB_DATA_MANAGER_H
#define CM_RDB_DATA_MANAGER_H

#include <mutex>

#include "rdb_helper.h"

#include "cm_rdb_config.h"

namespace OHOS {
namespace Security {
namespace CertManager {
class CmRdbDataManager : public std::enable_shared_from_this<CmRdbDataManager> {
public:
    CmRdbDataManager(const RdbConfig &rdbConfig);
    ~CmRdbDataManager();

    bool InsertData(const NativeRdb::ValuesBucket &valuesBucket);
    bool UpdateData(const std::string &primKey, const std::string &keyColumn,
        const NativeRdb::ValuesBucket &valuesBucket);
    bool DeleteData(const std::string &primKey, const std::string &keyColumn);
    std::shared_ptr<NativeRdb::AbsSharedResultSet> QueryData(const std::string &primKey, const std::string &keyColumn);

    bool CreateTable();
    void DelayCloseRdbStore();
    static void ClearCache();

private:
    std::shared_ptr<NativeRdb::RdbStore> GetRdbStore();

    RdbConfig rdbConfig_;
    std::mutex rdbMutex_;
    std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
};
} // namespace CertManager
} // namespace Security
} // namespace OHOS

#endif // CM_RDB_DATA_MANAGER_H